package cn.laixueit.controller;

import cn.laixueit.pojo.item.Brand;
import cn.laixueit.pojo.result.PageResult;
import cn.laixueit.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/page")
    public ResponseEntity<PageResult<Brand>> page(@RequestParam(value = "key",required = false)String key,
                                                  @RequestParam(value = "page",defaultValue = "1")Integer page,
                                                  @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                                  @RequestParam(value = "sortBy",required = false)String sortBy,
                                                  @RequestParam(value = "desc",required = false)Boolean desc){
        PageResult<Brand> result = brandService.findPage(key,page,rows,sortBy,desc);

        return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(@RequestBody Brand brand, @RequestParam("cids") List<Long> cids){
        this.brandService.saveBrand(brand, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据商品分类查询品牌信息
     */
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> findBrandListByCategoryId(@PathVariable("cid")Long cid){
        List<Brand> brandList = brandService.findBrandListByCategoryId(cid);
        return ResponseEntity.ok(brandList);
    }


    /**
     * 根据id查询对应brand
     */
    @GetMapping("/{id}")
    public ResponseEntity<Brand> getBrandById(@PathVariable("id") Long id){
        Brand brand = brandService.getById(id);
        return ResponseEntity.ok(brand);
    }
}
